﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DATN.Common;

namespace DATN.DataAccess
{
    public class SqlCommon
    {

        public static readonly string ConnectionString = AppConfig.ConnectionString;
       
        public int ExecuteNonquery(string queryString)
        {
            int result = 0;
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(queryString, connection) { CommandType = System.Data.CommandType.Text};
                
                cmd.Connection.Open();
                
                result = cmd.ExecuteNonQuery();

                cmd.Connection.Dispose();
            }

            return result;
        }

        public IDataReader ExecuteReader(string queryString)
        {
            IDataReader result = null;
            try
            {
                var conn = new SqlConnection(ConnectionString);
                conn.Open();

                var cmd = new SqlCommand(queryString, conn) { CommandType = CommandType.Text };
                result = cmd.ExecuteReader(CommandBehavior.CloseConnection);
               
                return result;
            }
            catch
            {
                if (result != null)
                {
                    result.Close();
                }
                throw;
            }            
        }

    }
}
